import tkinter as tk
from tkinter import messagebox
import json
import uids
import update
import get_data
import selftext as st
import logging
from tkinter import ttk

def get_all_uids():
    with open('userdata/uid.json', 'r') as f:
        data = json.load(f)
        data = list(data.values())
        return data


def chose_uid():
    uid_dict = get_all_uids()
    if not uid_dict:
        messagebox.showinfo("错误", "没有找到UID，请先添加UID")
        return
    global window
    window = tk.Tk()
    window.title("选择UID")

    uid_var = tk.StringVar(window)
    uid_var.set(uid_dict[0])  # 设置默认的UID

    uid_menu = tk.OptionMenu(window, uid_var, *uid_dict)
    uid_menu.configure(width=15, height=2, font=("Arial", 12))  # 设置OptionMenu的宽度和高度
    uid_menu.place(x=90, y=40)

    button = tk.Button(window, text="进入菜单", command=lambda: display_menu(uid_var.get()))
    button.configure(width=15, height=2, font=("Arial", 12))  # 设置Button的宽度和高度
    button.place(x=110, y=130)

    window.geometry("400x500")
    center_window(window)

    window.mainloop()

def display_menu(uid):
    global window
    window.destroy()

    window = tk.Tk()
    window.title("菜单")

    button1 = tk.Button(window, text="更新面板", command=lambda: update_user_data(uid))
    button1.place(x=40, y=20)

    button2 = tk.Button(window, text="角色总览", command=lambda: locality_data(uid))
    button2.place(x=40, y=70)

    button3 = tk.Button(window, text="角色详情", command=lambda: user_(uid))
    button3.place(x=40, y=150)

    button4 = tk.Button(window, text="返回上级", command=lambda: [window.destroy(), chose_uid()])
    button4.place(x=250, y=50)

    input_entry = tk.Entry(window)
    input_entry.configure(width=20, font=("Arial", 14))  # 设置输入框的宽度和字体大小
    input_entry.place(x=120, y=305)
    button5 = tk.Button(window, text="添加UID", command=lambda: add_uid(input_entry.get()))
    button5.place(x=40, y=300)

    uid_dict = get_all_uids()
    window.title("移除")

    combobox = ttk.Combobox(window, values=uid_dict, state="readonly")
    combobox.configure(width=18, font=("Arial", 14))  # 设置OptionMenu的宽度和高度
    combobox.place(x=120, y=350)

    button6 = tk.Button(window, text="移除UID", command=lambda: remove_uid(combobox.get()))
    button6.place(x=40, y=350)

    button0 = tk.Button(window, text="退出", command=window.destroy, font=("Arial", 12), width=10)
    button0.place(x=140, y=450)





    window.geometry("400x500")
    center_window(window)

    window.mainloop()

def update_user_data(uid):
    update.update_user_data(uid)

def locality_data(uid):
    get_data.locality_data(uid)


def user_(uid):
    cr,crid = get_data.user_(uid)

    window.title("角色选择")

    combobox = ttk.Combobox(window, values=cr, state="readonly")
    combobox.place(x=140, y=150)

    def get_selected_role(uid):
        selected_role = combobox.get()
        print("选中的角色:", selected_role)
        num = int(cr.index(selected_role))+1
        user__(uid, num)

    button = tk.Button(window, text="查询选中角色", command=lambda: (get_selected_role(uid)))
    button.place(x=40, y=200)


def user__(uid,num):
    get_data.roles(uid,num)

def add_uid(u):
    uids.add_uid(u)
    st.data_file()
    st.userdata_file()
    uid_dict = get_all_uids()
    combobox = ttk.Combobox(window, values=uid_dict, state="readonly")
    combobox.configure(width=18, font=("Arial", 14))  # 设置OptionMenu的宽度和高度
    combobox.place(x=120, y=350)

def remove_uid(uid):
    k,v = uids.print_all_uid()
    num = int(v.index(uid)) + 1
    uids.remove_uid(str(num))
    uid_dict = get_all_uids()
    window.title("移除")

    combobox = ttk.Combobox(window, values=uid_dict, state="readonly")
    combobox.configure(width=18, font=("Arial", 14))  # 设置OptionMenu的宽度和高度
    combobox.place(x=120, y=350)

def center_window(window):
    window.update_idletasks()
    width = window.winfo_width()
    height = window.winfo_height()
    x = (window.winfo_screenwidth() // 2) - (width // 2)
    y = (window.winfo_screenheight() // 2) - (height // 2)
    window.geometry('{}x{}+{}+{}'.format(width, height, x, y))

if __name__ == '__main__':
    chose_uid()
